package com.zzyl.member.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.member.domain.dto.ReservationDto;
import com.zzyl.member.domain.pojo.PageBean;
import com.zzyl.member.domain.pojo.Reservation;
import com.zzyl.member.service.SubscribeNursingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequestMapping("/member/reservation")
@RestController
@Api("预约")
public class SubscribeNursingController extends BaseController {
    @Autowired
    private SubscribeNursingService subscribeNursingService;
    @Autowired
    private DefaultRedisScript limitScript;

    @ApiOperation("新增预约")
    @PostMapping
    public AjaxResult addSubscribe(@RequestBody ReservationDto dto){
        subscribeNursingService.add(dto);
        return AjaxResult.success();
    }
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public R getPage(Integer pageNum, Integer pageSize, Integer status) {

        Page<Reservation> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Reservation> wrapper = Wrappers.<Reservation>lambdaQuery().eq(status != null, Reservation::getStatus, status);
        Page<Reservation> page1 = subscribeNursingService.page(page, wrapper);
        PageBean pageBean = new PageBean();
        pageBean.setTotal(page1.getTotal());
        pageBean.setRows(page1.getRecords());
        return R.ok(pageBean);
    }
















    @ApiOperation("查询当天取消预约数量")
    @GetMapping("/cancelled-count")
    public AjaxResult cancelledCount(){
        Long count = subscribeNursingService.serachcanCelledCount();
        return AjaxResult.success(count);
    }

    @ApiOperation("2.5 取消预约")
    @PutMapping("/{id}/cancel")
    public AjaxResult cancelledCount(@PathVariable Long id){
         subscribeNursingService.cancel(id);
        return AjaxResult.success();
    }
    @ApiOperation("查询每个时间段剩余预约次数")
    @GetMapping("/countByTime")
    public AjaxResult countByTime(){
        List<Map<String, Object>> count = subscribeNursingService.countByTime();
        return AjaxResult.success(count);
    }
}
